#include "UpdatePlayerNetworkEvent.h"
#include "ClientMenuState.h"
#include <cstring>

UpdatePlayerNetworkEvent::UpdatePlayerNetworkEvent(Action action, int playerId, const char* playerName,
        const Color& color)
{
    message.header.code = NetworkEvent::SERVER_PLAYER_UPDATE;
    message.header.sizeOfData = sizeof(message) - sizeof(message.header);
    message.data.action = action;
    message.data.playerId = playerId;
    message.data.playerColor = color;
    strncpy(message.data.playerName, playerName, sizeof(message.data.playerName));

}

UpdatePlayerNetworkEvent::UpdatePlayerNetworkEvent(const Header& header, const Data& data)
{
    message.header = header;
    message.data = data;
}

void* UpdatePlayerNetworkEvent::getMessageBlob()
{
    return &message;
}

size_t UpdatePlayerNetworkEvent::getMessageBlobSize()
{
    return sizeof(message);
}

GameState* UpdatePlayerNetworkEvent::visitClientMenuState(ClientMenuState* clientMenuState) const
{
    switch (message.data.action)
    {
    case UpdatePlayerNetworkEvent::ADD:
        return clientMenuState->addPlayerText(message.data.playerName, message.data.playerColor);

    case UpdatePlayerNetworkEvent::RESET:
        return clientMenuState->clearPlayerTexts();

    default:
        return NULL;
    }
}

UpdatePlayerNetworkEvent::~UpdatePlayerNetworkEvent()
{
}

